package xyz.ibenben.zhongdian.system.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import xyz.ibenben.zhongdian.common.annotation.SystemControllerLog;
import xyz.ibenben.zhongdian.common.constants.Constants;
import xyz.ibenben.zhongdian.system.entity.ChatRecord;
import xyz.ibenben.zhongdian.system.form.ChatRecordForm;
import xyz.ibenben.zhongdian.system.service.ChatRecordService;
import xyz.ibenben.zhongdian.system.service.impl.RedisService;

import java.util.List;

/**
 * 聊天记录控制类
 * 提供了一些基本的服务，如初始化、获取聊天记录列表等方法。
 * spring mvc思想 通过controller来调用service里的方法，
 * service里的方法再通过调用Dao来实现对数据的操作
 * 返回值统统是String类型的返回页面有统一的安排方便代码提取
 *
 * @author chenjian
 * @since 2017年9月27日上午10:57:28
 */
@Controller
@SessionAttributes("chatRecordForm")
@RequestMapping("/chatRecord")
public class ChatRecordController extends BaseController{
	/* 实体名称 */
	private static final String ENTITYNAME = "chatRecord";

	private final ChatRecordService chatRecordService;
	private final RedisService redisService;

	@Autowired
	public ChatRecordController(ChatRecordService chatRecordService, RedisService redisService) {
		this.chatRecordService = chatRecordService;
		this.redisService = redisService;
	}

	/**
	 * 初始化
	 * @param map 参数
	 * @param chatRecordForm 条件参数
	 */
	@ModelAttribute
	public void init(Model map, @ModelAttribute ChatRecordForm chatRecordForm){
		ChatRecordForm newChatRecordForm = new ChatRecordForm();
		if(null != chatRecordForm){
			newChatRecordForm = chatRecordForm;
		}
		map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newChatRecordForm);
	}

	/**
	 * 获取聊天记录列表
	 * @param map 参数
	 * @param chatRecordForm 条件参数
	 * @return 页面
	 */
	@RequestMapping(value="/getall")
	@SystemControllerLog(description = "获取聊天记录列表")
	public String getall(Model map, @ModelAttribute ChatRecordForm chatRecordForm){
		map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), chatRecordForm);
		Page<ChatRecord> page = PageHelper.startPage(chatRecordForm.getPageIndex(), chatRecordForm.getPageSize());
		List<ChatRecord> list = chatRecordService.selectByExample(chatRecordForm.buildSearchCommond());
		if(list != null && !list.isEmpty()){
			for(ChatRecord chat: list){
				if(null != chat.getFromId()){
					chat.setFromUserName(redisService.get(chat.getFromId().toString()).toString());
				}
				if(null != chat.getToId()){
					chat.setToUserName(redisService.get(chat.getToId().toString()).toString());
				}
			}
		}
		map.addAttribute(String.format(Constants.LISTENTITY, ENTITYNAME), list);
		map.addAttribute(Constants.PI, page.toPageInfo());
		return String.format(Constants.LISTPAGE, ENTITYNAME);
	}

	/**
	 * 浏览聊天记录记录
	 * @param map 参数
	 * @param id 主键
	 * @param chatRecordForm 条件参数
	 * @return 页面
	 */
	@RequestMapping(value="/{id}/view", method= RequestMethod.GET)
	@SystemControllerLog(description = "浏览聊天记录")
	public String openViewPage(Model map, @PathVariable Long id,
			@ModelAttribute ChatRecordForm chatRecordForm){
		ChatRecord chat = chatRecordService.selectByKey(id);
		if(null != chat.getFromId()){
			chat.setFromUserName(redisService.get(chat.getFromId().toString()).toString());
		}
		if(null != chat.getToId()){
			chat.setToUserName(redisService.get(chat.getToId().toString()).toString());
		}
		map.addAttribute(ENTITYNAME, chat);
		return String.format(Constants.VIEWPAGE, ENTITYNAME);
	}
	
}

